package com.jy.common;

import com.jy.model.User;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

public class SessionContext {
	private static SessionContext instance;
    private HashMap<String,HttpSession> sessionMap;
     
    private SessionContext() {
        sessionMap = new HashMap<String,HttpSession>();
    }    
    
    public static SessionContext getInstance() {
        if (instance == null) {    
            instance = new SessionContext();
        }    
        return instance;    
    }    
    
    /*public synchronized void AddSession(HttpSession session) {
        if (session != null) {    
            sessionMap.put(session.getId(), session);
        }    
    } */
    
    public  void DelSession(HttpSession session) {
    	
        if (session != null) {    
            sessionMap.remove(session.getId());    
            if(session.getAttribute(SessionInfo.SessionName)!=null){   
            	User user=(User)session.getAttribute(SessionInfo.SessionName);
                sessionMap.remove(user.getId());
                //session.invalidate();     
            }    
        }
    }
    
    public synchronized HttpSession getSession(String session_id) {
        if (session_id == null) return null;    
        return (HttpSession) sessionMap.get(session_id);
    }    
    
    public HashMap getSessionMap() {    
        return sessionMap;    
    }    
    
    public void setMymap(HashMap sessionMap) {    
        this.sessionMap = sessionMap;    
    }

    /**
     * 用户和Session绑定关系
     */
    public static final Map<String, HttpSession> USER_SESSION = new HashMap<String, HttpSession>();

    /**
     * seeionId和用户的绑定关系
     */
    public static final Map<String, String> SESSIONID_USER = new HashMap<String, String>();

}
